package com.lexian.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lexian.entity.Goodscollectio;
import com.lexian.service.GoodscollectioService;
import com.lexian.utils.CommonResponse;
import com.lexian.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 商品收藏表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/goodscollectio")
public class GodscollectioController {

    @Autowired
    private GoodscollectioService goodscollectioService;
    //添加收藏
    @GetMapping("collectio/{gid}")
    public CommonResponse collectio(@PathVariable Long gid, HttpServletRequest request){
        goodscollectioService.saveCollectic(gid,request);
        return CommonResponse.ok();
    }

    //取消收藏
    @GetMapping("cancel/{gid}")
    public CommonResponse cancel(@PathVariable Long gid, HttpServletRequest request){
        //获取顾客id，根据顾客id和商品id查出收藏记录，然后删除
        //获取顾客编号
        //String userId = JwtUtils.getMemberIdByJwtToken(request);
        //模拟一个顾客编号
        Long userId = 2L;
        //根据顾客编号和商品id查出收藏的记录
        //条件
        QueryWrapper<Goodscollectio> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",gid);
        wrapper.eq("cus_id",userId);
        wrapper.select("goodscollectio_id");
        //调用查询方法
        Goodscollectio goodscollectio = goodscollectioService.getOne(wrapper);
        //通过收藏id删除收藏
        goodscollectioService.removeById(goodscollectio.getGoodscollectioId());
        return CommonResponse.ok();
    }




}

